#coding=utf-8

from django import forms
from vote.models import Vote


    
class VoteForm(forms.Form):
    result  = forms.ChoiceField(label=u"action", choices=Vote.VOTE_CHOICES)
    opinion = forms.CharField(label=u"opinion", widget=forms.Textarea, required=False)
    
    warn = ""
    
    def __init__(self, request=None, *args, **kwargs):
        super(VoteForm, self).__init__(*args, **kwargs)
        
    def save(self, user, question):
        result = int(self.cleaned_data['result'])
        opinion = self.cleaned_data.get('opinion', '')
        if opinion == '':
            has_opinion = False
        else:
            has_opinion = True
        vote = Vote(user=user, question=question,result=result, opinion=opinion, has_opinion=has_opinion)
        vote.save()
        
        return vote